package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import model.MeetingInfo;

/**
 * This is a Class to display meeting information
 */
public class MeetingDetailDisplayPanel extends JPanel
{
    /**
     * 
     */
    private static final long serialVersionUID = -2771095008104303020L;
    
    MailDisplayArea mailDisplayArea;
    MeetingInfo meeting;
    MeetingData mData;
    String status = "";
    
    /**
     * Constructor
     * @param MailDisplayArea mailDisplayArea
     * @param MeetingInfo meeting
     */
    public MeetingDetailDisplayPanel(MailDisplayArea mailDisplayArea, MeetingInfo meeting)
    {
        this.mailDisplayArea = mailDisplayArea;
        this.meeting = meeting;
        
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
                
        mData = new MeetingData();
        this.add(mData);
      
    }
    
    /**
     * set TitledBorder with String of status
     * @param status the status of the meeting
     */
    public void setTitleBorder(String status)
    {
        TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Meeting Status = " + status);
        this.setBorder(title);
        
        this.revalidate();
        this.repaint(); 
    }
    /**
     * get MeetingInfo in MeetingDetailDisplayPanel
     * @return MeetingInfo meeting
     */
    public MeetingInfo getMeetingInfo()
    {
        return this.meeting;
    }
    
    /**
     * display Participants status in MeetingDetailDisplayPanel
     */
    public void loadRecepientsDetail()
    {
       
       String status = "";
       
       for(String userID : this.meeting.receivers)
       {
           int userHashCode = userID.hashCode();
           Map<Integer, String> meetingInfo = this.meeting.meetingParticipantData;
           status = meetingInfo.get(userHashCode);

           MeetingReceiptStatus recStatusPanel = new MeetingReceiptStatus(userID, status);
           this.add(recStatusPanel);
       }
       
       if(status.equals(MeetingInfo.MEETING_CANCELLED))
       {
           this.setTitleBorder("Cancelled");
       }
       else
       {
           this.setTitleBorder("Active"); 
       }
 
       this.revalidate();
       this.repaint(); 
    }
    
    /**
     * get meeting message
     * @return the meeting message
     */
    public String getMessage()
    {
        return mData.messageTextField.getText();
    }
    
    /**
     * get meeting subject
     * @return return the meeting subject
     */
    public String getSubject()
    {
        return mData.subjectTextField.getText();
    }
    
    /**
     * get meeting place
     * @return return meeting place
     */
    public String getPlace()
    {
        return mData.locationTextField.getText();
    }
    
    /**
     * get meeting start time
     * @return return meeting start time
     */
    public String getStartTime()
    {
        String startTime  = mData.startYear.getSelectedItem() + "_" + mData.startMonth.getSelectedItem() + "_" + 
                mData.startDay.getSelectedItem() +" " + mData.startHour.getSelectedItem() + ":" + mData.startMinute.getSelectedItem() + ":" + "00";
        return startTime;
   }
    
    /**
     * get meeting end time
     * @return return meeting end time
     */
    public String getEndTime()
    {
        String endTime  = mData.endYear.getSelectedItem() + "_" + mData.endMonth.getSelectedItem() + "_" + 
                mData.endDay.getSelectedItem() +" " + mData.endHour.getSelectedItem() + ":" + mData.endMinute.getSelectedItem() + ":" + "00";
        return endTime;
    }
    
    /**
     * This is a Class to display meeting receipt status
     */
    class MeetingReceiptStatus extends JPanel
    {
        /**
         * 
         */
        private static final long serialVersionUID = 6144890465014087326L;
        
        /**
         * Constructor
         * @param recepient the recipient
         * @param status the status of the meeting request of recipient
         */
        public MeetingReceiptStatus(String recepient, String status)
        {
          TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"Meeting Participant : " + recepient + ", Status : " + status);
          this.setBorder(title);
          this.setLayout(null);
          this.setMinimumSize(new Dimension(1500,30));
          this.setMaximumSize(new Dimension(1500,30));        
        }
        
    }
    
    /**
     * create a meeting data class
     */
    class MeetingData extends JPanel
    {
        /**
         * 
         */
        private static final long serialVersionUID = 6144890465014087326L;
        
        private JTextField subjectTextField;
        private JTextArea messageTextField;
        private JTextField locationTextField;
        
        private JComboBox startDay;
        private JComboBox startMonth;
        private JComboBox startYear;
        private JComboBox startHour;
        private JComboBox startMinute;
        
        private JComboBox endDay;
        private JComboBox endMonth;
        private JComboBox endYear;
        private JComboBox endHour;
        private JComboBox endMinute;
        
        /**
         * Constructor of MeetingData
         */
        public MeetingData()
        {
          TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black));
          this.setBorder(title);
          this.setLayout(null);
          this.setMaximumSize(new Dimension(1500,500));
          this.setMaximumSize(new Dimension(1500,500));
      
              JLabel subjectLable = new JLabel("Subject");
              subjectLable.setBounds(new Rectangle(32,04,64,30));
              subjectLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
              this.add(subjectLable);
              
              JLabel locationLable = new JLabel("Location");
              locationLable.setBounds(new Rectangle(32,44,64,30));
              locationLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
              this.add(locationLable);
              
              JLabel startLable = new JLabel("StartTime");
              startLable.setBounds(new Rectangle(32,84,64,30));
              startLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
              this.add(startLable);
              
              JLabel endLable = new JLabel("EndTime");
              endLable.setBounds(new Rectangle(32,124,64,30));
              endLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
              this.add(endLable);
              
              JLabel contentLable = new JLabel("Message");
              contentLable.setBounds(new Rectangle(32,164,64,30));
              contentLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
              this.add(contentLable);
              
              
              subjectTextField = new JTextField();
              subjectTextField.setText(MeetingDetailDisplayPanel.this.meeting.originalSubject);
              subjectTextField.setBounds(new Rectangle(135,04,400,30));
              this.add(subjectTextField);
              
              locationTextField = new JTextField();
              locationTextField.setText(MeetingDetailDisplayPanel.this.meeting.origianlPlace);
              locationTextField.setBounds(new Rectangle(135,44,400,30));
              this.add(locationTextField);
              
              String[] date ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31", "Start"};
              startDay = new JComboBox(date);
              startDay.setBounds(135, 94, 50, 20);
              startDay.addActionListener(new ActionListener()
               {
                  public void actionPerformed(ActionEvent e) 
                  {
                      String selection = (String)startDay.getSelectedItem();
                  }
                  
               });
               
              this.add(startDay);       

              String[] month ={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
              startMonth = new JComboBox(month);
              startMonth.setBounds(195, 94, 60, 20);
              startMonth.addActionListener(new ActionListener()
               {
                  public void actionPerformed(ActionEvent e) 
                  {
                      String selection = (String)startMonth.getSelectedItem();
                  }
                  
               });
               
              this.add(startMonth);    
              
              String[] yeardata ={"2013","2014","2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025","2026","2027","2028","2029","2030"};
              startYear = new JComboBox(yeardata);
              startYear.setBounds(250, 94, 65, 20);
              startYear.addActionListener(new ActionListener()
               {
                  public void actionPerformed(ActionEvent e) 
                  {
                      String selection = (String)startYear.getSelectedItem();
                  }
                  
               });
               
              this.add(startYear);
              
              JLabel lblNewLabelDay = new JLabel("DD");
              lblNewLabelDay.setBounds(155, 74, 46, 14);
              this.add(lblNewLabelDay);
              
              JLabel lblMonth = new JLabel("MM");
              lblMonth.setBounds(200, 74, 46, 14);
              this.add(lblMonth);
              
              JLabel lblYear = new JLabel("YY");
              lblYear.setBounds(255, 74, 46, 14);
              this.add(lblYear);
              
              String[] hour ={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};
              startHour = new JComboBox(hour);
              startHour.setBounds(315, 94, 47, 20);
              startHour.addActionListener(new ActionListener()
               {
                  public void actionPerformed(ActionEvent e) 
                  {
                      String selection = (String)startHour.getSelectedItem();
                  }
                  
               });
               
              this.add(startHour);
              
              String[] min ={"00","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
              startMinute = new JComboBox(min);
              startMinute.setBounds(360, 94, 52, 20);
              startMinute.addActionListener(new ActionListener()
               {
                  public void actionPerformed(ActionEvent e) 
                  {
                      String selection = (String)startMinute.getSelectedItem();
                  }
                  
               });
               
              this.add(startMinute);            
                      
               JLabel lblNewLabelHour = new JLabel("HH");
               lblNewLabelHour.setBounds(320, 74, 46, 14);
               this.add(lblNewLabelHour);
                  
               JLabel lblMin = new JLabel("MIN");
               lblMin.setBounds(365, 74, 46, 14);
               this.add(lblMin);
                                   
                   String[] date1 ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31"};
                      endDay = new JComboBox(date1);
                      endDay.setBounds(135, 124, 50, 20);
                      endDay.addActionListener(new ActionListener()
                       {
                          public void actionPerformed(ActionEvent e) 
                          {
                              String selection = (String)endDay.getSelectedItem();
                          }
                          
                       });
                       
                      this.add(endDay);
                  

                      String[] month1 ={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
                      endMonth = new JComboBox(month1);
                      endMonth.setBounds(195, 124, 60, 20);
                      endMonth.addActionListener(new ActionListener()
                       {
                          public void actionPerformed(ActionEvent e) 
                          {
                              String selection = (String)endMonth.getSelectedItem();
                          }
                          
                       });
                       
                      this.add(endMonth);
                      
                      
                  
                      String[] year1 ={"2013","2014","2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025","2026","2027","2028","2029","2030"};
                      endYear = new JComboBox(year1);
                      endYear.setBounds(250, 124, 65, 20);
                      endYear.addActionListener(new ActionListener()
                       {
                          public void actionPerformed(ActionEvent e) 
                          {
                              String selection = (String)endYear.getSelectedItem();
                          }
                          
                       });
                       
                      this.add(endYear);
     
                      String[] hour1 ={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};
                      endHour = new JComboBox(hour1);
                      endHour.setBounds(315, 124, 47, 20);
                      endHour.addActionListener(new ActionListener()
                       {
                          public void actionPerformed(ActionEvent e) 
                          {
                              String selection = (String)endHour.getSelectedItem();
                          }
                          
                       });
                       
                      this.add(endHour);  
                      
                      String[] min1 ={"00","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
                      endMinute = new JComboBox(min1);
                      endMinute.setBounds(360, 124, 52, 20);
                      endMinute.addActionListener(new ActionListener()
                       {
                          public void actionPerformed(ActionEvent e) 
                          {
                              String selection = (String)endMinute.getSelectedItem();
                          }
                          
                       });
                       
                      this.add(endMinute); 
                      
                      
                      String start = MeetingDetailDisplayPanel.this.meeting.originalStartTime;
                      
                      String[] split1 = start.split(" ");
                      String[] startDate = split1[0].split("_");
                      String[] startTime = split1[1].split(":");
                      
                      startDay.setSelectedItem(startDate[2]);
                      startMonth.setSelectedItem(startDate[1]);
                      startYear.setSelectedItem(startDate[0]);
                      startHour.setSelectedItem(startTime[0]);
                      startMinute.setSelectedItem(startTime[1]);
                 
                      String end = MeetingDetailDisplayPanel.this.meeting.originalEndiTime;
                      String[] split2 = end.split(" ");
                      String[] endDate = split2[0].split("_");
                      String[] endTime = split2[1].split(":");
                      
                      endDay.setSelectedItem(endDate[2]);
                      endMonth.setSelectedItem(endDate[1]);
                      endYear.setSelectedItem(endDate[0]);
                      endHour.setSelectedItem(endTime[0]);
                      endMinute.setSelectedItem(endTime[1]);                   
          
              messageTextField = new JTextArea();
              messageTextField.setText(MeetingDetailDisplayPanel.this.meeting.originalMessage);
              JScrollPane scroll = new JScrollPane (messageTextField, 
                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
              scroll.setBounds(new Rectangle(135,164,400,220));    
              this.add(scroll);            
        }
    }
}
